// Author: wsfuyibing <682805@qq.com>
// Date: 2024-12-02

package location

import (
	"context"
	"gitee.com/go-libs/result"
	"gitee.com/go-libs/validator-iris/validator"
	"gitee.com/go-wares/framework-iris/framework/src/logics"
	"github.com/kataras/iris/v12"
	"server/app/lib"
)

// Logic
// 基于坐标反查地图位置.
type Logic struct {
	request *Request
}

// New
// 创建逻辑.
func New() logics.Logical {
	return &Logic{
		request: &Request{},
	}
}

// Before
// 前置校验.
func (o *Logic) Before(_ context.Context, i iris.Context) (err error) {
	err = validator.Query(i, o.request)
	return
}

// Run
// 业务处理.
func (o *Logic) Run(ctx context.Context, _ iris.Context) result.Result {
	var (
		err error
		res = result.New()
		ptr any
	)
	if ptr, err = lib.Maps.GetAddressByLocation(ctx, o.request.Latitude, o.request.Longitude); err != nil {
		return res.WithError(err)
	}
	return res.With(ptr)
}
